home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’97 / Warrior’s Progress / source code / Source / Libraries / Arrays / ConstArrayOf.h < prev   
Encoding:
Text File  |  1997-06-28  |  2.6 KB  |  87 lines  |  [TEXT/CWIE]

  1. // ConstArrayOf.h
  2.  
  3. #ifndef ConstArrayOf_h
  4. #define ConstArrayOf_h
  5.  
  6. #ifndef Integers_h
  7. #include "Integers.h"
  8. #endif
  9. #ifndef Assert_h
  10. #include "Assert.h"
  11. #endif
  12. #ifndef Range_h
  13. #include "Range.h"
  14. #endif
  15.  
  16. template < class Element >
  17. class ConstArrayOf
  18.   {
  19.     typedef ConstArrayOf< Element > ConstArrayType;
  20.  
  21.     private:
  22.         const Element *start;
  23.         uint32 length;
  24.     
  25.     public:
  26.         ConstArrayOf()
  27.           : start( 0 ),
  28.             length( 0 )
  29.           {}
  30.         
  31.         ConstArrayOf( const Element *theStart, uint32 theLength )
  32.           : start( theStart ),
  33.              length( theLength )
  34.           { Assert( theStart != 0 || length == 0 ); }
  35.  
  36.         const Element *Start() const                        { return start; }
  37.         const Element *End() const                            { return start + length; }
  38.  
  39.         uint32 Length() const                                { return length; }
  40.         URange32 Range() const                                { return URange32( 0, Length() ); }
  41.                 
  42.         uint32 BoundedLength( uint32 bound ) const
  43.             { return ( length <= bound ) ? length : bound; }
  44.  
  45.         void LimitLength( uint32 limit )                    { length = BoundedLength( limit ); }
  46.         
  47.         bool IsEmpty() const                                    { return length == 0; }
  48.         bool Null() const                                        { return start == 0; }
  49.         
  50.         const Element& operator[]( uint32 i ) const    { Assert( i < length ); return start[i]; }
  51.         
  52.         ConstArrayType Head( uint32 position ) const;
  53.         ConstArrayType Tail( uint32 position ) const;
  54.         ConstArrayType Middle( URange32 ) const;
  55.         
  56.         void Truncate( uint32 position );                // *this = Head( *this, position );
  57.         void Shorten( uint32 amount );                    // *this = Tail( *this, amount )
  58.  
  59.         void Append( ConstArrayType tail );
  60.         void Prepend( ConstArrayType head );
  61.         
  62.         void operator+=( ConstArrayType tail )            { Append( tail ); }
  63.         ConstArrayType operator+( ConstArrayType tail ) const;
  64.         
  65.         bool Precedes( ConstArrayType r ) const        { return End() == r.start; }
  66.         bool Follows( ConstArrayType r ) const            { return start == r.End(); }
  67.         
  68.         bool IsHeadOf( ConstArrayType r ) const;
  69.         bool IsTailOf( ConstArrayType r ) const;
  70.         
  71.         // These operators express the containment ordering
  72.         bool operator==( ConstArrayType r ) const        { return start == r.start && length == r.length; }
  73.         bool operator!=( ConstArrayType r ) const        { return start != r.start || length != r.length; }
  74.         bool operator<=( ConstArrayType r ) const;
  75.         bool operator>=( ConstArrayType r ) const        { return r <= *this; }
  76.         bool operator<( ConstArrayType r ) const;
  77.         bool operator>( ConstArrayType r ) const        { return r < *this; }
  78.         
  79.         bool Touches( ConstArrayType r ) const;
  80.         ConstArrayType operator&( ConstArrayType r ) const;
  81.         ConstArrayType operator|( ConstArrayType r ) const;
  82.         void operator&=( ConstArrayType r )                { *this = *this & r; }
  83.         void operator|=( ConstArrayType r )                { *this = *this | r; }
  84.   };
  85.  
  86. #endif
  87.